//引入路由(todo:这里为什么在这里这么引入)
import router from '@router'
//引入store
import { useInfoStore } from '@store/useUserStore'
import { useMenuStore } from '@store/useMenuStore'
//引入interface(字段定义)
import { ILoginRequest } from '@interface/login'

//引入element
import { ElMessage } from 'element-plus'

const useLogin = async (res: ILoginRequest) => {
  //判断是否登录成功，不成功给出错误提示
  if (res.code != '200') {
    return ElMessage.error(res.msg)
  }
  //1.登录成功后持久化存储token
  const token = res.data
  localStorage.setItem('TOKEN', token || '')

  //2.获取用户信息
  await useInfoStore().getUserInfo()

  //3.获取路由(角色权限)
  await useMenuStore().getUserMenu()

  //4.跳转到首页
  router.push('/')

  return
}

export default useLogin
